#include "KSJCamera.h"

KSJCamera::KSJCamera(int width=700, int height=500, int exposure=10)
{
	nRet = 0;
	KSJ_TRIGGERMODE   mode = KSJ_TRIGGER_INTERNAL;
	KSJ_TRIGGERMETHOD method = KSJ_TRIGGER_FALLINGEDGE;

	// 初始化相机
	nRet = KSJ_Init();
	printf(" %s %s %d     %d   initdone \n", __FILE__, __FUNCTION__, __LINE__, nRet);

	if (nRet != 0)
	{
		printf(" %s %s %d     %d   initdone \n", __FILE__, __FUNCTION__, __LINE__, nRet);

		exit(1);
	}

	// 取得相机的个数
	nCamCount = KSJ_DeviceGetCount();

	if (nCamCount <= 0)
	{
		printf("KSJ_DeviceGetCount failed: %d!   \n", nCamCount);

		nRet = KSJ_UnInit();
		exit(1);
	}

	printf(" %s %s %d   count  %d    \n", __FILE__, __FUNCTION__, __LINE__, nCamCount);

#ifdef _WIN32
	Sleep(2);
#else
	sleep(2);
#endif
	int i;
	unsigned short pusDeviceType;
	int pnSerials;
	unsigned short pusFirmwareVersion;
	unsigned short pusFpgaVersion;

	for (i = 0; i < nCamCount; ++i)
	{
		// 取得相机的信息
		nRet = KSJ_DeviceGetInformationEx(i, &pusDeviceType, &pnSerials, &pusFirmwareVersion, &pusFpgaVersion);

		if (nRet != 0)
		{
			printf("%d Device KSJ_DeviceGetInformationEx failed! %s %s %d nRet  %d  \n", i, __FILE__, __FUNCTION__, __LINE__, nRet);

			nRet = KSJ_UnInit();
			exit(1);
		}

		printf("%d Device Info: 0x%08X - 0x%08X - 0x%08X - 0x%08X\n", i, pusDeviceType, pnSerials, pusFirmwareVersion, pusFpgaVersion);

		// 设置相机触发模式
		KSJ_TriggerModeSet(i, mode);
		printf(" %s %s %d   Device %d KSJ_TriggerModeSet mode = %d\n", __FILE__, __FUNCTION__, __LINE__, i, mode);

		KSJ_TriggerModeGet(i, &mode);
		printf(" %s %s %d   Device %d  KSJ_GetTriggerMode  mode = %d\n", __FILE__, __FUNCTION__, __LINE__, i, mode);

		// 设置触发方式
		KSJ_TriggerMethodSet(i, method);
		printf(" %s %s %d   Device %d  KSJ_TriggerModeSet mode = %d\n", __FILE__, __FUNCTION__, __LINE__, i, mode);

		KSJ_TriggerMethodGet(i, &method);
		printf(" %s %s %d   Device %d  KSJ_GetTriggerMode  mode = %d\n", __FILE__, __FUNCTION__, __LINE__, i, mode);
		
		


		// 设置曝光行
		KSJ_ExposureTimeSet(i, exposure);
		printf(" %s %s %d   Device %d  KSJ_TriggerModeSet exp = %d\n", __FILE__, __FUNCTION__, __LINE__, i, exposure);

		float fexp;
		KSJ_ExposureTimeGet(i, &fexp);
		printf(" %s %s %d   Device %d  KSJ_GetTriggerMode  exp = %0.2f\n", __FILE__, __FUNCTION__, __LINE__, i, fexp);


		//KSJ_SetParam(i, KSJ_MIRROR, 0);
		
		KSJ_BayerSetMode(0,KSJ_RGGB_BGR24_FLIP);
		//KSJ_BayerSetMode(i,KSJ_GRBG_BGR24_FLIP);
		//KSJ_BayerSetMode(i,KSJ_RGGB_BGR24_FLIP);
		//KSJ_BayerSetMode(i,KSJ_GBRG_BGR24_FLIP);

		if (width > 0 && height > 0)
		{
			int nMaxWidth, nMaxHeight;

			// 取得最大的视场
			if (KSJ_CaptureGetDefaultFieldOfView(i, NULL, NULL, &nMaxWidth, &nMaxHeight, NULL, NULL) == 0)
			{
				printf(" Max W&H is W=%d, H=%d\n", nMaxWidth, nMaxHeight);

				if (width <= nMaxWidth && height <= nMaxHeight)
				{
					// 设置视场
					KSJ_CaptureSetFieldOfView(i, (nMaxWidth - width) / 2, (nMaxHeight - height) / 2, width, height, KSJ_SKIPNONE, KSJ_SKIPNONE);
				}
			}
		}
	}

	int nIndex = 0;

	

	// 取得图像的大小
	nRet = KSJ_CaptureGetSizeEx(0, &nWidth, &nHeight, &nBitCount);

	if (nRet != 0)
	{
		printf(" %s %s %d     %d   KSJ_CaptureGetSizeEx failed \n", __FILE__, __FUNCTION__, __LINE__, nRet);

		nRet = KSJ_UnInit();
		exit(1);
	}

	printf("===== W=%d, H=%d, bc=%d =====\r\n",nWidth, nHeight, nBitCount);
	// 分配内存
	pImageBuffer = new unsigned char[nWidth * nHeight * nBitCount / 8];

	//    goto HEAD; //test init
}


KSJCamera::~KSJCamera()
{
	delete[]pImageBuffer;
	pImageBuffer = NULL;
	nRet = KSJ_UnInit();
	printf(" %s %s %d     %d   uninitdone \n", __FILE__, __FUNCTION__, __LINE__, nRet);
}

bool KSJCamera::getSrc(Mat & img)
{
		//  获取图像
		nRet = KSJ_CaptureRgbData(0, pImageBuffer);

		if (nRet == 0)
		{
				// 处理
				if (nBitCount == 8) img = cv::Mat(nHeight, nWidth, CV_8UC1, pImageBuffer, nWidth);
				else img = cv::Mat(nHeight, nWidth, CV_8UC3, pImageBuffer, nWidth * 3);

				return true;

		}

	printf("===== capture failed! =====\r\n");
	return false;
}
int KSJCamera::get_width()
{
	return nWidth;
}
int KSJCamera::get_height()
{
	return nHeight;
}